
package com.fivebrms.view.w9
{
	import org.osflash.thunderbolt.Logger;
	
	import mx.controls.Button;
	import mx.controls.CheckBox;
	import mx.controls.TextInput;
	import mx.containers.VBox;
	import mx.controls.Alert;
	
	import mx.events.FlexEvent;
	import mx.binding.utils.BindingUtils;
	
	import mx.validators.StringValidator;
		
	import flash.events.Event;
	import flash.events.MouseEvent;
	import com.fivebrms.events.W9FormSendEvent;
	import com.fivebrms.model.MainModelLocator;
	import com.fivebrms.view.w9.validators.W9FormValidator;
	import mx.events.ValidationResultEvent;
	import mx.validators.ValidationResult;
	
	public class FormFields extends VBox
	{
		private var model : MainModelLocator;
		
		public var indivPartCont : VBox;
		
		public var chk_indivOrSolePropr : CheckBox;
		public var chk_partnership : CheckBox;
		public var inp_indivPartnName : TextInput;
		public var inp_indivPartnSocialSecurityNumb : TextInput;
		
		public var corpBusCont : VBox;

		public var chk_corporation : CheckBox;
		public var chk_business : CheckBox;
		public var inp_corpBusBusinessName : TextInput;
		public var inp_corpBusEmployIdentNumb : TextInput;
		public var inp_corpBusStreetAddress : TextInput;
		
		public var inp_signature : TextInput;
		
		public var btn_submitW9Form : Button;
		
		private var validationResult : ValidationResultEvent;
		
		private var w9FormValidate : W9FormValidator;
		
		//
		// constructor
		//
		public function FormFields()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
		}
		
        private function creationCompleteHandler(event:FlexEvent) : void
        {
            Logger.info("w9 form fields opened..1..");
            
            this.model = MainModelLocator.getInstance();
            
            //
            // validate the form before allowing form to be submitted
            // show user warnings on user interface input controls
            // create only one instance of this object
            //
        	w9FormValidate = new W9FormValidator( this );

            Logger.info("w9 form fields opened..2..");
			//
			// submit the form data to the server
			//
			btn_submitW9Form.addEventListener(MouseEvent.CLICK, buttonPressedHandler);

            Logger.info("w9 form fields opened..3..");
            Logger.info("w9 form fields opened..3.1." + this.chk_indivOrSolePropr);
            Logger.info("w9 form fields opened..3.2." + this.chk_indivOrSolePropr.className );
            
			//
			// validate fields as the forms are inputed 
			//
        	this.chk_indivOrSolePropr.addEventListener(Event.CHANGE, this.validateW9Form);

        	this.chk_partnership.addEventListener(Event.CHANGE, this.validateW9Form);

        	this.inp_indivPartnName.addEventListener(Event.CHANGE, this.validateW9Form);

        	this.inp_indivPartnSocialSecurityNumb.addEventListener(Event.CHANGE, this.validateW9Form);

        	this.chk_corporation.addEventListener(Event.CHANGE, this.validateW9Form);

        	this.chk_business.addEventListener(Event.CHANGE, this.validateW9Form);

        	this.inp_corpBusBusinessName.addEventListener(Event.CHANGE, this.validateW9Form);

 	      	this.inp_corpBusEmployIdentNumb.addEventListener(Event.CHANGE, this.validateW9Form);

        	this.inp_corpBusStreetAddress.addEventListener(Event.CHANGE, this.validateW9Form);

        	this.inp_signature.addEventListener(Event.CHANGE, this.validateW9Form);
        	
            Logger.info("w9 form fields opened..5..");
		}
		
		private function buttonPressedHandler(event : MouseEvent ) : void
		{
            Logger.info("you clicked the btn_submitW9Form button!!!");
            
            
            if ( validationResult.results == null )
            {
	            //
	            // submit the w9 form to the server
	            //            
	            var w9Event : W9FormSendEvent = new W9FormSendEvent( );
	            w9Event.dispatch();
	            
	            Alert.show("The form has been successfully submitted.");
	            
	            //
	            // disable the whole entire control so that form cannot be
	            // double submitted
	            //
	            this.enabled = false;
	        }
            else
            {
            	Alert.show("Could not submit form.\nNot all required fields have\nbeen entered.");
            }
            
            Logger.info("done btn_submitW9Form event..!");
		}
		
		//
		// this will attach a label stating whether or not the input field
		// needs to be entered
		//
		private function validateW9Form( event : Event ) : void
		{
			Logger.info("start - hello...1");

			this.inp_indivPartnName.errorString = "";
			this.inp_indivPartnSocialSecurityNumb.errorString = "";
			this.inp_corpBusBusinessName.errorString = "";
			this.inp_corpBusEmployIdentNumb.errorString = "";
			this.inp_corpBusStreetAddress.errorString = "";
			this.inp_signature.errorString = "";
			
			//
			// copy form fields to value object so that it may be transported 
			// to the server
			//
			this.model.w9FormVo.isIndividual = this.chk_indivOrSolePropr.selected;
			this.model.w9FormVo.isPartnership = this.chk_partnership.selected;
			this.model.w9FormVo.indivPartName = this.inp_indivPartnName.text;
			this.model.w9FormVo.indivPartSsNum = this.inp_indivPartnSocialSecurityNumb.text;

			this.model.w9FormVo.isCorporation = this.chk_corporation.selected;
			this.model.w9FormVo.isBusiness = this.chk_business.selected;
			this.model.w9FormVo.corpBusName = this.inp_corpBusBusinessName.text;
			this.model.w9FormVo.corpBusIdentification = this.inp_corpBusEmployIdentNumb.text;
			this.model.w9FormVo.corpBusAddress = this.inp_corpBusStreetAddress.text;
			
			this.model.w9FormVo.signature = this.inp_signature.text;
					
			this.validationResult = w9FormValidate.validate( event.currentTarget );
			
			var results : Array = validationResult.results;
			
			for (var x : int; x < results.length; x++)
			{
			
				Logger.info("-1->" + event.currentTarget.id );
				Logger.info("-3->" + results[x].errorCode );
				Logger.info("-4->" + results[x].errorMessage );
				Logger.info("-5->" + results[x].isError );
				Logger.info("-6->" + results[x].subField );
				Logger.info("<------------------->" );
				
				if
					(
						( results[x].subField == "inp_indivPartnName" )
					)
				{

					Logger.info("hey now...1..");

					this.inp_indivPartnName.errorString = results[x].errorMessage;
				}		
				if
					(
						( results[x].subField == "inp_indivPartnSocialSecurityNumb" )
					)
				{
					Logger.info("hey now...2..");

					this.inp_indivPartnSocialSecurityNumb.errorString = results[x].errorMessage;
				}		
				if
					(
						( results[x].subField == "inp_corpBusBusinessName" )
					)
				{
					this.inp_corpBusBusinessName.errorString = results[x].errorMessage;
				}		
				if
					(
						( results[x].subField == "inp_corpBusEmployIdentNumb" )
					)
				{
					this.inp_corpBusEmployIdentNumb.errorString = results[x].errorMessage;
				}	
				if
					(
						( results[x].subField == "inp_corpBusStreetAddress" )
					)
				{
					this.inp_corpBusStreetAddress.errorString = results[x].errorMessage;
				}
				if
					(
						( results[x].subField == "inp_signature" )
					)
				{
					this.inp_signature.errorString = results[x].errorMessage;
				}
				
				Logger.info("cool.." + x + " <-> " + results[x].errorCode );
				Logger.info("cool.." + x + " <-> " + results[x].errorMessage );
				Logger.info("cool.." + x + " <-> " + results[x].isError );
				Logger.info("cool.." + x + " <-> " + results[x].subField );

			}
			
			Logger.info("end - hello...2");
			
		}
  		
	}
}

